//引入依赖express包
const express = require("express");
const app = express();
//引入端口
const port = 3000;

const jwt = require("jsonwebtoken");
const JWT_SECRET_KEY = "JWT_SECRET_KEY";
const Response = require("./utils/Response");

//引入 Joi模块 表单验证

//配置跨域的问题
const cors = require("cors");
app.use(
  cors({
    //传参指定构造的时候放行所有的跨域
    origin: "*",
  })
);
//解析POST请求参数
app.use(express.urlencoded());

// 自定义token全局验证中间件
const tokenTools = function (req, resp, next) {
  // 若请求路径是 /user/login 则不拦截，直接向后执行即可
  if (req.path == "/user/login") {
    next();
    return;
  }

  // 执行token验证
  let token = req.headers["authorization"];
  try {
    let payload = jwt.verify(token, JWT_SECRET_KEY);
    req.tokenPayload = payload; // 将token中存储的数据，直接复制给req，这样在后续业务中就可以使用req.tokenPayload获取这些信息
  } catch (error) {
    resp.send(Response.error(401, "用户验证失败，请重新登录"));
    return;
  }
  next(); // 继续后续业务的执行
};
app.use(tokenTools);

//引入外部路由 router中的MovieActor
// let movieActorRouter = require('./router/MovieActor.js')
// app.use(movieActorRouter)
//简写
app.use(require("./router/MovieActor.js"));
app.use(require("./router/MovieDirector.js"));
app.use(require("./router/MovieInfo.js"));
app.use(require("./router/MovieThumb.js"));
app.use(require("./router/Cinema.js"));
app.use(require("./router/CinemaRoom.js"));
app.use(require("./router/ShowingonPlan.js"));
app.use(require("./router/Admin.js"));
//构造接口  处理/请求
app.get("/", (req, res) => {
  res.send("Hello!");
});

app.listen(port, () => {
  console.log("影城后端服务已启动");
});
